package com.kemorebi.forum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kemorebi.forum.common.R;
import com.kemorebi.forum.common.context.BaseContextHandler;
import com.kemorebi.forum.model.dto.Register2Dto;
import com.kemorebi.forum.model.dto.UpdatePsdDTO;
import com.kemorebi.forum.model.dto.UserSimDTO;
import com.kemorebi.forum.model.dto.UserUpdateDTO;
import com.kemorebi.forum.model.entity.User;
import com.kemorebi.forum.mapper.UserMapper;
import com.kemorebi.forum.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public Boolean checkUserExist(String acount) {
        LambdaQueryWrapper<User> lbq = new LambdaQueryWrapper<>();
        lbq.eq(User::getAccount, acount);
        return baseMapper.exists(lbq);
    }

    @Override
    public R<Boolean> register2(Register2Dto register2Dto) {
        LambdaUpdateWrapper<User> userLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userLambdaUpdateWrapper.eq(User::getUid, register2Dto.getUid());
        userLambdaUpdateWrapper.set(User::getMobile,register2Dto.getMobile());
        userLambdaUpdateWrapper.set(User::getEmail,register2Dto.getEmail());
        update(userLambdaUpdateWrapper);
        log.error(BaseContextHandler.getUserId().toString());
        return R.success();
    }


    @Override
    public Boolean updateUser(UserUpdateDTO dto) {
        LambdaUpdateWrapper<User> lbu = new LambdaUpdateWrapper<>();
        lbu.eq(User::getUid, dto.getUid())
                .set(User::getNickname, dto.getNickname())
                .set(User::getMobile, dto.getMobile());
        if (dto.getAge()!= null) {
            lbu.set(User::getAge, dto.getAge());
        }

        if (dto.getGender() != null) {
            lbu.set(User::getGender, dto.getGender());
        }

        if (dto.getEmail() != null) {
            lbu.set(User::getEmail, dto.getEmail());
        }

        if (dto.getAvatar() != null) {
            lbu.set(User::getAvator, dto.getAvatar());
        }

        if (dto.getDescription() != null) {
            lbu.set(User::getDescription, dto.getDescription());
        }
        update(lbu);
        log.info("用户{}信息修改成功, 修改参数为:{}", dto.getUid(), dto);
        return true;
    }
    @Override
    public R<Boolean> updateUserPassword(UpdatePsdDTO updatePsdDTO) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUid,updatePsdDTO.getUid()).
                eq(User::getPassword, DigestUtils.md5Hex( updatePsdDTO.getOldPassword()));
        if (getOne(userLambdaQueryWrapper)==null){
            return R.fail("原密码错误!");
        }
        LambdaUpdateWrapper<User> lbu = new LambdaUpdateWrapper<>();
        lbu.eq(User::getUid, updatePsdDTO.getUid())
                .set(User::getPassword, DigestUtils.md5Hex(updatePsdDTO.getNewPassword()));
        update(lbu);
        return R.success();
    }
    @Override
    public List<UserSimDTO> queryUser(String query) {
        return baseMapper.queryUser(query);
    }
}
